<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Tool extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function captcha()
	{
		if (isset($this->session->img_captcha['filename']) && $this->session->img_captcha['filename']) {
			unlink(FCPATH . 'captcha/' . $this->session->img_captcha['filename']);
		}

		$vals = array(
			'img_path'    => './captcha/',
			'img_url'     => base_url('/captcha/'),
			'font_path'   => './fonts/simhei.ttf',
			'img_width'   => '100',
			'img_height'  => 39,
			'expiration'  => 7200,
			'word_length' => 4,
			'font_size'   => 20,
			'img_id'      => 'Imageid',
			'pool'        => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
			'colors'      => array(
				'background' => array(255, 255, 255),
				'border'     => array(255, 255, 255),
				'text'       => array(0, 0, 0),
				'grid'       => array(255, 40, 40)
				)
			);
		$cap = create_captcha($vals);
		$this->session->img_captcha = $cap;
		echo $cap['image'];
	}

	public function email_safe()
	{
		$this->verify_user_ajax();
		$email = $this->user['email'];

		if (!is_email($email)) {
			ajax_return(1, '找不到邮箱地址');
		}

		$captcha = rand(100000, 999999);
		$this->session->old_email_captcha = $captcha;
		$this->send_email($email, $captcha);
		ajax_return(0, '邮件已发送，请注意查收，如果没有收到，请重新发送');
	}

	public function email_check()
	{
		$this->verify_url_ajax();
		$email = $this->input->post('email');

		if (!is_email($email)) {
			ajax_return(1, '邮箱地址无效');
		}

		$exist = $this->db->where('email', $email)->count_all_results('user');

		if (!$exist) {
			ajax_return(1, '找不到邮箱地址');
		}

		$this->session->email = $email;
		$captcha = rand(100000, 999999);
		$this->session->email_captcha = $captcha;
		$this->send_email($email, $captcha);
		ajax_return(0, '邮件已发送，请注意查收，如果没有收到，请重新发送');
	}

	public function email_owner()
	{
		$this->verify_url_ajax();
		$email = $this->input->post('email');

		if (!is_email($email)) {
			ajax_return(1, '邮箱地址无效');
		}

		$this->session->email = $email;
		$captcha = rand(100000, 999999);
		$this->session->email_captcha = $captcha;
		$this->send_email($email, $captcha);
		ajax_return(0, '邮件已发送，请注意查收，如果没有收到，请重新发送');
	}

	public function mobile_owner()
	{
		$this->verify_url_ajax();
		$this->load->helper('mobile');
		$mobile = $this->input->post('mobile');

		if (!is_mobile($mobile)) {
			ajax_return(1, '手机号码无效');
		}

		$this->session->mobile = $mobile;
		$captcha = rand(100000, 999999);
		$this->session->mobile_captcha = $captcha;
		$username = $this->sys['mobile_username'];
		$password = $this->sys['mobile_password'];
		$content = '验证码：' . $captcha . '，请及时完成验证。【QBIT】';

		if (send_mobile($username, $password, $mobile, $content)) {
			ajax_return(0, '短信验证码已发送，请注意查收，如果没有收到，请重新发送');
		}
		else {
			ajax_return(1, '短信验证码发送失败，请重新发送');
		}
	}

	public function mobile_safe()
	{
		$this->verify_url_ajax();
		$this->load->helper('mobile');
		$mobile = $this->user['mobile'];

		if (!is_mobile($mobile)) {
			ajax_return(1, '请认证手机');
		}

		$captcha = rand(100000, 999999);
		$this->session->old_mobile_captcha = $captcha;
		$username = $this->sys['mobile_username'];
		$password = $this->sys['mobile_password'];
		$content = '验证码：' . $captcha . '，请及时完成验证。【QBIT】';

		if (send_mobile($username, $password, $mobile, $content)) {
			ajax_return(0, '短信验证码已发送，请注意查收，如果没有收到，请重新发送');
		}
		else {
			ajax_return(1, '短信验证码发送失败，请重新发送');
		}
	}
}

?>
